//
//  TWTitleView.swift
//  EyepetizerImitative
//
//  Created by 田伟 on 2017/4/17.
//  Copyright © 2017年 个人. All rights reserved.
//

import UIKit

protocol titleViewDelegate : NSObjectProtocol {
    func TitleViewTitleButtonDidClick(index : Int)              // 当前点击的是第几个按钮
    func TitleViewPreviousClickTitleButton(index : Int)         // 第几个按钮被重复点击
}

class TWTitleView: UIView {

    fileprivate lazy var titleViewUnderLine : UILabel = UILabel()
    
    // MARK:- 属性
    var previousClickTitleButton : TWTitleButton?                                           // 上一次点击的标题按钮
    var titleArray : [TWDiscoverTitleModel]?{
        didSet {
            guard let titleArray = titleArray else {
                return
            }
            setupCategeryButton(array: titleArray)
        }
    }
    var delegate : titleViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.white.withAlphaComponent(0.9)
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        backgroundColor = UIColor.white.withAlphaComponent(0.9)
//        fatalError("init(coder:) has not been implemented")
    }
}

// MARK:- UI界面
extension TWTitleView {
    fileprivate func setupCategeryButton(array : [TWDiscoverTitleModel]){
        // 添加按钮
        var i = 0
        let BtnW = UIScreen.main.bounds.width / CGFloat(array.count)
        for titleModel in array {
            let button = TWTitleButton(type: .custom)
            button.setTitle(titleModel.name, for: .normal)
            button.frame = CGRect(x: CGFloat(i) * BtnW, y: 0, width: BtnW, height: self.frame.height)
            button.addTarget(self, action: #selector(titleButtonDidClick(button:)), for: .touchUpInside)
            addSubview(button)
            i += 1
        }
        // 添加分割线
        let bottomLine = UILabel(frame: CGRect(x: 0, y: self.frame.height - 0.5, width: self.frame.width, height: 0.5))
        bottomLine.backgroundColor = UIColor.lightGray.withAlphaComponent(0.5)
        addSubview(bottomLine)
        // 设置下划线
        for view in self.subviews {
            if view.isKind(of: TWTitleButton.self)  {
                let firstBtn : TWTitleButton = self.subviews.first as! TWTitleButton
                firstBtn.isSelected = true
                previousClickTitleButton = firstBtn
                titleViewUnderLine.frame = CGRect(x: firstBtn.center.x - 4, y: firstBtn.frame.height - 5, width: 8, height: 3)

            }
        }
        titleViewUnderLine.backgroundColor = UIColor.black
        addSubview(titleViewUnderLine)
    }
    @objc func titleButtonDidClick(button : TWTitleButton){
        // 当前按钮的索引
        let index = self.subviews.index(of: button)! as Int
        if previousClickTitleButton == button {
            delegate?.TitleViewPreviousClickTitleButton(index: index)
        }
        previousClickTitleButton?.isSelected = false
        button.isSelected = true
        previousClickTitleButton = button
        
        UIView.animate(withDuration: viewAnimationTime, animations: {
            // 1.移动下划线
            self.titleViewUnderLine.center.x = button.center.x
        }) { (_) in
            // 当前点击的是第几个按钮
            self.delegate?.TitleViewTitleButtonDidClick(index: index)
        }
    }
}
